package de.gwtprime.server.model;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import de.gwtprime.server.sql.helper.HibernateHelper;

@Entity
@Table(name="article_comment")
public class ArticleComment extends EntityBase {

	/**
	 * SerialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	private String text;
	private User2 user;
	
	private String userName;
	private Date date = new Date();
	
	private Article article;
	
	public ArticleComment(){
		
	}
	
	@ManyToOne
	@JoinColumn
	public Article getArticle() {
		return article;
	}
	
	public void setArticle(Article article) {
		this.article=article;
	}
	
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	
//	@OneToOne
//	public User getUser() {
//		return user;
//	}
//	public void setUser(User user) {
//		this.user = user;
//	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	public static ArticleComment findArticleComment(Long id) {
		return HibernateHelper.find(ArticleComment.class, id);
	}
	
	public void remove(){
		long id = getArticle().getId();
		super.remove();
		HibernateHelper.find(Article.class, id).decreaseNumberCommentsAndPersist();
	}
	

	@Override
	public String toString() {
		return "ArticleComment [id=" + getId() + "]";
	}

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn
	public User2 getUser() {
		return user;
	}

	public void setUser(User2 user) {
		this.user = user;
		if(user!=null)
			this.userName = user.getEmail();
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

}
